package com.day1019.snake;

import java.awt.event.KeyEvent;
import java.util.LinkedList;

//  🐍类
public class Snake {
//    多个节点 身体
    private LinkedList<Node> body;//  身体
    private Direction direction;// 移动方向
    private boolean isLive = true;// 表明🐍是活的

    public boolean isLive() {
        return isLive;
    }

    public void setLive(boolean live) {
        isLive = live;
    }

    public Snake() {
        body = new LinkedList<>();
        Node foot = new Node(20,20);
        Node bod = new Node(21,20);
        Node head = new Node(22,20);
        body.add(head);
        body.add(bod);
        body.add(foot);
        direction = Direction.RIGHT;
    }

    public LinkedList<Node> getBody() {
        return body;
    }

    public void setBody(LinkedList<Node> body) {
        this.body = body;
    }

    public Direction getDirection() {
        return direction;
    }

    public void setDirection(Direction direction) {
        this.direction = direction;
    }

    public void move(){
        Node head = this.body.getFirst();
        switch (direction){
            case RIGHT:
                this.body.removeLast();// 删除尾巴
                Node nodeR = new Node(head.getX()+1,head.getY());
                this.body.addFirst(nodeR);

                break;
            case LEFT:
                this.body.removeLast();
                Node nodeL = new Node(head.getX()-1,head.getY());
                this.body.addFirst(nodeL);
                break;
            case UP:
                this.body.removeLast();
                Node nodeU = new Node(head.getX(),head.getY()-1);
                this.body.addFirst(nodeU);
                break;
            case DOWN:
                this.body.removeLast();
                Node nodeD = new Node(head.getX(),head.getY()+1);
                this.body.addFirst(nodeD);
                break;
        }
    }

    public void changeDirection(int keyCode){

        switch (keyCode){
            case KeyEvent.VK_UP:
                this.setDirection(Direction.UP);
                break;
            case KeyEvent.VK_DOWN:
                this.setDirection(Direction.DOWN);
                break;
            case KeyEvent.VK_LEFT:
                this.setDirection(Direction.LEFT);
                break;
            case KeyEvent.VK_RIGHT:
                this.setDirection(Direction.RIGHT);
                break;
        }
    }

    //  穿墙
//    public void throughTheWall(){
//        Node head = this.getBody().getFirst();
//        int x = head.getX();
//        int y = head.getY();
//        if(head.getY() < 0){
//            head.setX(head.getX());
//        }
//    }

    //  🐍是否die
    public void checkOver(){
        Node head = this.body.getFirst();
        int x = head.getX();
        int y = head.getY();
        if(head.getX() < 0){
            head.setX(head.getY());
        }
        if(head.getX() > 41){
            head.setX(head.getY());
        }
        if(head.getY() > 42){
            head.setY(head.getX());
        }
        if(head.getY() < 0){
            head.setY(head.getX());
        }

        for (int i = 1; i < this.body.size(); i++) {
            if (head.getX() == this.body.get(i).getX() && head.getY() == this.body.get(i).getY()){
                this.isLive = false;
            }
        }



    }

    public void eat(Food food){
        Node head = this.getBody().getFirst();
        Node foot = this.getBody().getLast();
        if (head.getX() == food.getX() && head.getY() == food.getY()){
            this.getBody().add(foot);
            GamePanel.food = new Food();
        }
    }
}
